package com.zmn.plat.admin.controller.brand.ratio;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.plat.admin.controller.PlatBaseController;
import com.zmn.plat.business.interfaces.brand.ratio.BrandRatioBService;
import com.zmn.plat.common.dictionary.BaseBrandRatioTypeEnum;
import com.zmn.plat.manager.interfaces.cache.AreaCache;
import com.zmn.plat.model.entity.brand.BrandRatio;
import com.zmn.plat.model.entity.brand.BrandRatioQuery;
import com.zmn.plat.model.entity.servcategory.ServCategory;
import com.zmn.plat.model.vo.brand.BrandRatioVo;
import com.zmn.plat.model.vo.brand.factor.BrandFactorCategoryRatioVo;
import com.zmn.plat.services.interfaces.brand.BaseBrandService;
import com.zmn.plat.services.interfaces.brand.BrandRatioService;
import com.zmn.plat.services.interfaces.category.BaseCategoryService;
import com.zmn.plat.services.interfaces.company.BaseCompanyAreaService;
import com.zmn.plat.services.interfaces.company.BaseCompanyService;
import com.zmn.plat.services.interfaces.product.ServProductService;
import com.zmn.plat.services.interfaces.servcategory.ServAssCategoryService;
import com.zmn.plat.services.interfaces.servcategory.ServCategoryService;

/**
 * 类描述:品牌系数管理baseContoller
 *
 * @author linfeng
 * @since 2019/11/21/11:00
 */
public class BrandRatioBaseController extends PlatBaseController {
    @Resource
    protected BaseCompanyService baseCompanyService;

    @Resource
    protected BrandRatioService brandRatioService;

    @Resource
    protected BrandRatioBService brandRatioBService;

    @Resource
    protected BaseBrandService baseBrandService;

    @Resource
    protected ServAssCategoryService servAssCategoryService;

    @Resource
    protected BaseCompanyAreaService baseCompanyAreaService;

    @Resource
    protected ServCategoryService servCategoryService;

    @Resource
    protected BaseCategoryService baseCategoryService;

    @Resource
    protected ServProductService servProductService;

    @Resource
    protected AreaCache areaCache;

    /**
     * 保内
     */
    private final int IN_SIDE = 0;

    /**
     * 保外
     */
    private final int OUT_SIDE = 1;

    /**
     * 品牌分类系数(一二级分类系数对应表)
     *
     * @param brandId
     * @param servCategId
     * @param bizType
     * @param brandName
     * @return
     */
    @SuppressWarnings("Duplicates")
    public ModelAndView categoryRatio(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "bizType", defaultValue = "1") int bizType,
        @RequestParam(value = "brandName", defaultValue = "") String brandName) {
        BrandFactorCategoryRatioVo brandFactorCategoryRatioVo =
            brandRatioBService.findByBrandIdAndServCategId(brandId, priceType, servCategId, bizType, brandName);

        ModelAndView mav = new ModelAndView("brand/ratio/categoryratio");

        mav.addObject("servCategId", brandFactorCategoryRatioVo.getServCategId());
        mav.addObject("brandId", brandId);
        mav.addObject("bizType", bizType);
        mav.addObject("priceType", priceType);
        mav.addObject("brandName", brandName);
        mav.addObject("servCategName", brandFactorCategoryRatioVo.getServCategName());
        mav.addObject("categOnes", brandFactorCategoryRatioVo.getBrandLevelOneRatioVos());
        mav.addObject("servCategoryList", brandFactorCategoryRatioVo.getServCategories());
        mav.addObject("map", brandFactorCategoryRatioVo.getMap());
        return mav;
    }

    /**
     * 设置分类系数的详情页面
     *
     * @param brandId
     * @param servCategId
     * @param categOneId
     * @param categOneName
     * @param bizType
     * @param brandName
     * @param servCategName
     * @return
     */
    @SuppressWarnings("Duplicates")
    public ModelAndView detail(@RequestParam(value = "brandId", defaultValue = "0") int brandId,
        @RequestParam(value = "priceType", defaultValue = "1") int priceType,
        @RequestParam(value = "servCategId", defaultValue = "0") int servCategId,
        @RequestParam(value = "categOneId", defaultValue = "0") int categOneId,
        @RequestParam(value = "categOneName", defaultValue = "0") String categOneName,
        @RequestParam(value = "bizType", defaultValue = "1") int bizType,
        @RequestParam(value = "brandName", defaultValue = "") String brandName,
        @RequestParam(value = "servCategName", defaultValue = "") String servCategName) {
        BrandRatioQuery brandRatioQuery = new BrandRatioQuery();
        brandRatioQuery.setBrandId(brandId);
        brandRatioQuery.setBizType(bizType);
        brandRatioQuery.setRatioType(BaseBrandRatioTypeEnum.BRANDCATEGONERATIO.getCode());
        brandRatioQuery.setPriceType(priceType);
        brandRatioQuery.setServCategId(servCategId);
        brandRatioQuery.setCategOneId(categOneId);

        if (servCategId == 0) {
            ServCategory servCategory = servCategoryService.findFirstOneByCategIdAsc();
            brandRatioQuery.setServCategId(servCategory.getCategId());
        }

        String categOneValue = "";
        String categOneToBeEffectiveValue = "";
        List<BrandRatio> brandLevelOneRatioVos = brandRatioService.listByQuery(brandRatioQuery);
        if (brandLevelOneRatioVos != null && brandLevelOneRatioVos.size() > 0) {
            BrandRatio brandRatio = brandLevelOneRatioVos.get(0);
            categOneValue = brandRatio.getRatio() == null ? "" : brandRatio.getRatio().toString();
            categOneToBeEffectiveValue = brandRatio.getToBeEffectiveRatio() == null ? "" : brandRatio.getToBeEffectiveRatio().toString();
        }
        brandRatioQuery.setRatioType(BaseBrandRatioTypeEnum.BRANDCATEGTWORATIO.getCode());
        List<BrandRatioVo> resultList = brandRatioBService.listBrandLevelTwoRatioVo(brandRatioQuery);
        ModelAndView mav = new ModelAndView("brand/ratio/categoryratiodetail");

        mav.addObject("servCategId", servCategId);
        mav.addObject("priceType", priceType);
        mav.addObject("brandId", brandId);
        mav.addObject("brandName", brandName);
        mav.addObject("categOneId", categOneId);
        mav.addObject("bizType", bizType);
        mav.addObject("categOneName", categOneName);
        mav.addObject("categOneValue", categOneValue);
        mav.addObject("categOneToBeEffectiveValue", categOneToBeEffectiveValue);
        mav.addObject("servCategName", servCategName);
        mav.addObject("levelTwoRatio", resultList);
        return mav;
    }

}
